﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Web.Configuration;

namespace HospitalSystem
{
    public partial class Report2 : System.Web.UI.Page
    {
        
        //public static List<string> x = new List<string>();
        //public static List<string> x2 = new List<string>();
        //public static List<string> x3 = new List<string>();

        //public static List<string> x4 = new List<string>();
        //public static List<string> x5 = new List<string>();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //string CID = (String)Session["CID"];
                try
                {
                    string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
                    SqlConnection scon = new SqlConnection(constr);
                    string CIDs = Request.QueryString["IDStudent"];
                    string teacher = "";
                    Label1.Text = CIDs;
                    scon.Open();
                    SqlCommand cmd = new SqlCommand("select cid,FirstName,LastName,DOB,Gender,BloodType,Nation,Race,RecordedStaffID from person where cid = @ParamCID", scon);
                    cmd.Parameters.AddWithValue("@ParamCID", CIDs);
                    SqlDataReader reader = cmd.ExecuteReader();
                    while (reader.Read() != false)
                    {
                        Label2.Text = reader["FirstName"].ToString();
                        Label3.Text = reader["LastName"].ToString();
                        Label4.Text = reader["DOB"].ToString();
                        Label5.Text = reader["Gender"].ToString();
                        Label6.Text = reader["BloodType"].ToString();
                        Label7.Text = reader["Nation"].ToString();
                        Label8.Text = reader["Race"].ToString();
                        teacher = reader["RecordedStaffID"].ToString();
                        //เหลือประเภทความพิการและที่อยู่ ยังไม่ได้ลง Label
                    }
                    reader.Close();
                    SqlCommand cmd1 = new SqlCommand("select StaffName from Staff where StaffID = @TeacherName", scon);
                    cmd1.Parameters.AddWithValue("@TeacherName", teacher);
                    SqlDataReader reader2 = cmd1.ExecuteReader();
                    while (reader2.Read() != false)
                    {
                        Label9.Text = reader2["StaffName"].ToString();
                    }
                    reader2.Close();
                    scon.Close();
                    //BindDropdownList();
                    BindGridview1();
                    Label10.Text = GridView1.Rows.Count.ToString();
                }
                catch(Exception)
                {
                    Label1.Text = "ERROR!!!!!!!!!!!!";
                }
            }
        }

        private void MessageBox(string msg)
        {
            Label lbl = new Label();
            lbl.Text = "<script language='javascript'>" + Environment.NewLine + "window.alert('" + msg + "')</script>";
            Page.Controls.Add(lbl);
        }

        private void BindGridview1()
        {
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection scon = new SqlConnection(constr);
            scon.Open();
            DataTable dt = new DataTable();
            dt.Columns.Add("CaseNo");
            dt.Columns.Add("EvalDateTime");
            SqlCommand cmd = new SqlCommand("select Evaluation.CaseNo,Evaluation.EvalDateTime from Person inner join Evaluation on Person.CID = Evaluation.CID where Person.CID = @PersonID", scon);
            cmd.Parameters.AddWithValue("@PersonID", Label1.Text);
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read() != false)
            {
                dt.Rows.Add(reader["CaseNo"],reader["EvalDateTime"]);
            }
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }

        public string GetUrl(object CaseNo)
        {
            string url = "ReportDetailScreening.aspx?IDStudent=" + Label1.Text + "&CaseNo=" + Server.UrlEncode(CaseNo.ToString());

            return url;
        }
        //private void BindDropdownList()
        //{
        //    //string CID = (String)Session["CID"];
        //    x.Clear();
        //    x2.Clear();
        //    DropDownList1.Items.Clear();
        //    DataTable dt = new DataTable();
        //    dt.Columns.Add("CaseNo");
        //    dt.Columns.Add("EvalDate");
        //    scon.Open();
        //    SqlCommand cmd2 = new SqlCommand("select Evaluation.CaseNo,Evaluation.EvalDateTime from Person inner join Evaluation on Person.CID = Evaluation.CID where Person.CID = @PersonID", scon);
        //    cmd2.Parameters.AddWithValue("@PersonID", Label1.Text);
        //    SqlDataReader reader3 = cmd2.ExecuteReader();
        //    while (reader3.Read() != false)
        //    {
        //        x.Add(reader3["CaseNo"].ToString());
        //        x2.Add(reader3["EvalDateTime"].ToString());
        //    }
        //    reader3.Close();
        //    if (x.Count() == 0)
        //    {
        //        DropDownList1.Items.Add(new ListItem("==เด็กคนนี้ยังไม่เคยผ่านการคัดกรอง==","0"));
        //    }
        //    else
        //    {
        //        DropDownList1.Items.Add(new ListItem("===เลือกครั้งที่ประเมิน===", "0"));
        //        for (int i = 0; i < x.Count(); i++)
        //        {
        //            dt.Rows.Add();
        //            dt.Rows[i]["CaseNo"] = x[i];
        //            dt.Rows[i]["EvalDate"] = x2[i];
        //        }

        //        for (int i = 0; i < dt.Rows.Count; i++)
        //        {
        //            string temp2 = dt.Rows[i]["CaseNo"].ToString();
        //            string temp = "เคสที่ "+dt.Rows[i]["CaseNo"]+"  ประเมินเมื่อ "+dt.Rows[i]["EvalDate"];
        //            DropDownList1.Items.Add(new ListItem(temp,temp2 ));
        //        }
        //    }
        //    Label10.Text = dt.Rows.Count.ToString();
        //    scon.Close();
        //}

        //protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    string caseNumber = DropDownList1.SelectedValue;
        //    string CID = Request.QueryString["IDStudent"];
        //    scon.Open();
        //    if (caseNumber == "0")
        //    {
        //        //ไม่ให้ทำอะไร
        //    }
        //    else
        //    {
        //        //แสดงออกทาง Gridview โดยมี 4 แบบ คือ
        //        //1. การคัดกรอง
        //        BindKudKrong(caseNumber, CID);
        //        //2. การวางแผน
        //        BindPlanning(caseNumber,CID);
                
        //    }
        //    scon.Close();
        //}

        //private void BindKudKrong(string caseNumber, string CID)
        //{
            
        //    DataTable dt = new DataTable();
        //    dt.Columns.Add("Evaluation");
        //    dt.Columns.Add("RecordStaff");
        //    dt.Columns.Add("EvalDateTime");


        //    string tempa = "";
        //    string tempb = "";
        //    int indexrow = 0;


        //    //Query in DisabilityEvaluation
        //    SqlCommand cmd1 = new SqlCommand("select RecorderID,EvalDateTime from DisabilityEvaluation where CID = @CID and CaseNo = @CaseNo", scon);
        //    cmd1.Parameters.AddWithValue("@CID", CID);
        //    cmd1.Parameters.AddWithValue("@CaseNo", caseNumber);
        //    SqlDataReader reader1 = cmd1.ExecuteReader();
        //    while (reader1.Read() != false)
        //    {
        //        tempa = reader1["RecorderID"].ToString();
        //        tempb = reader1["EvalDateTime"].ToString();
        //    }
        //    reader1.Close();
        //    if (tempa.Equals(""))
        //    {
        //        ;
        //    }
        //    else
        //    {
        //        SqlCommand cmd2 = new SqlCommand("select StaffName from Staff where StaffID = @StaffID", scon);
        //        cmd2.Parameters.AddWithValue("@StaffID", tempa);
        //        SqlDataReader reader2 = cmd2.ExecuteReader();
        //        while (reader2.Read() != false)
        //        {
        //            tempa = reader2["StaffName"].ToString();
        //        }
        //        reader2.Close();
        //        dt.Rows.Add();
        //        dt.Rows[indexrow]["Evaluation"] = "ความบกพร่อง";
        //        dt.Rows[indexrow]["RecordStaff"] = tempa;
        //        dt.Rows[indexrow]["EvalDateTime"] = tempb;
        //        indexrow++;
        //    }
        //    //========================================================
        //    tempa = "";
        //    tempb = "";
        //    //Query in HearEvaluation
        //    SqlCommand cmd3 = new SqlCommand("select RecorderID,EvalDateTime from HearEvaluation where CID = @CID and CaseNo = @CaseNo", scon);
        //    cmd3.Parameters.AddWithValue("@CID", CID);
        //    cmd3.Parameters.AddWithValue("@CaseNo", caseNumber);
        //    SqlDataReader reader3 = cmd3.ExecuteReader();
        //    while (reader3.Read() != false)
        //    {
        //        tempa = reader3["RecorderID"].ToString();
        //        tempb = reader3["EvalDateTime"].ToString();
        //    }
        //    reader3.Close();
        //    if (tempa.Equals(""))
        //    {
        //        ;
        //    }
        //    else
        //    {
        //        SqlCommand cmd4 = new SqlCommand("select StaffName from Staff where StaffID = @StaffID", scon);
        //        cmd4.Parameters.AddWithValue("@StaffID", tempa);
        //        SqlDataReader reader4 = cmd4.ExecuteReader();
        //        while (reader4.Read() != false)
        //        {
        //            tempa = reader4["StaffName"].ToString();
        //        }
        //        reader4.Close();
        //        dt.Rows.Add();
        //        dt.Rows[indexrow]["Evaluation"] = "ด้านได้ยิน";
        //        dt.Rows[indexrow]["RecordStaff"] = tempa;
        //        dt.Rows[indexrow]["EvalDateTime"] = tempb;
        //        indexrow++;
        //    }
        //    //========================================================
            
        //    tempa = "";
        //    tempb = "";
        //    //Query SightEvaluation
        //    SqlCommand cmd5 = new SqlCommand("select RecorderID,EvalDateTime from SightEvaluation where CID = @CID and CaseNo = @CaseNo", scon);
        //    cmd5.Parameters.AddWithValue("@CID", CID);
        //    cmd5.Parameters.AddWithValue("@CaseNo", caseNumber);
        //    SqlDataReader reader5 = cmd5.ExecuteReader();
        //    while (reader5.Read() != false)
        //    {
        //        tempa = reader5["RecorderID"].ToString();
        //        tempb = reader5["EvalDateTime"].ToString();
        //    }
        //    reader5.Close();
        //    if (tempa.Equals(""))
        //    {
        //        ;
        //    }
        //    else
        //    {
        //        SqlCommand cmd6 = new SqlCommand("select StaffName from Staff where StaffID = @StaffID", scon);
        //        cmd6.Parameters.AddWithValue("@StaffID", tempa);
        //        SqlDataReader reader6 = cmd6.ExecuteReader();
        //        while (reader6.Read() != false)
        //        {
        //            tempa = reader6["StaffName"].ToString();
        //        }
        //        reader6.Close();
        //        dt.Rows.Add();
        //        dt.Rows[indexrow]["Evaluation"] = "ด้านการมองเห็น";
        //        dt.Rows[indexrow]["RecordStaff"] = tempa;
        //        dt.Rows[indexrow]["EvalDateTime"] = tempb;
        //        indexrow++;
        //    }
        //    //========================================================
        //    //PhysicalEvaluation
        //    tempa = "";
        //    tempb = "";
        //    //Query SightEvaluation
        //    SqlCommand cmd7 = new SqlCommand("select RecorderID,EvalDateTime from PhysicalEvaluation where CID = @CID and CaseNo = @CaseNo", scon);
        //    cmd7.Parameters.AddWithValue("@CID", CID);
        //    cmd7.Parameters.AddWithValue("@CaseNo", caseNumber);
        //    SqlDataReader reader7 = cmd7.ExecuteReader();
        //    while (reader7.Read() != false)
        //    {
        //        tempa = reader7["RecorderID"].ToString();
        //        tempb = reader7["EvalDateTime"].ToString();
        //    }
        //    reader7.Close();
        //    if (tempa.Equals(""))
        //    {
        //        ;
        //    }
        //    else
        //    {
        //        SqlCommand cmd8 = new SqlCommand("select StaffName from Staff where StaffID = @StaffID", scon);
        //        cmd8.Parameters.AddWithValue("@StaffID", tempa);
        //        SqlDataReader reader8 = cmd8.ExecuteReader();
        //        while (reader8.Read() != false)
        //        {
        //            tempa = reader8["StaffName"].ToString();
        //        }
        //        reader8.Close();
        //        dt.Rows.Add();
        //        dt.Rows[indexrow]["Evaluation"] = "ด้านร่างกาย";
        //        dt.Rows[indexrow]["RecordStaff"] = tempa;
        //        dt.Rows[indexrow]["EvalDateTime"] = tempb;
        //        indexrow++;
        //    }

        //    //========================================================
        //    //FavorList
        //    tempa = "";
        //    tempb = "";
        //    //Query FavorList
        //    SqlCommand cmd9 = new SqlCommand("select RecorderID,EvalDateTime from FavorList where CID = @CID and CaseNo = @CaseNo", scon);
        //    cmd9.Parameters.AddWithValue("@CID", CID);
        //    cmd9.Parameters.AddWithValue("@CaseNo", caseNumber);
        //    SqlDataReader reader9 = cmd9.ExecuteReader();
        //    while (reader9.Read() != false)
        //    {
        //        tempa = reader9["RecorderID"].ToString();
        //        tempb = reader9["EvalDateTime"].ToString();
        //    }
        //    reader9.Close();
        //    if (tempa.Equals(""))
        //    {
        //        ;
        //    }
        //    else
        //    {
        //        SqlCommand cmd10 = new SqlCommand("select StaffName from Staff where StaffID = @StaffID", scon);
        //        cmd10.Parameters.AddWithValue("@StaffID", tempa);
        //        SqlDataReader reader10 = cmd10.ExecuteReader();
        //        while (reader10.Read() != false)
        //        {
        //            tempa = reader10["StaffName"].ToString();
        //        }
        //        reader10.Close();
        //        dt.Rows.Add();
        //        dt.Rows[indexrow]["Evaluation"] = "สิ่งที่ชอบ";
        //        dt.Rows[indexrow]["RecordStaff"] = tempa;
        //        dt.Rows[indexrow]["EvalDateTime"] = tempb;
        //        indexrow++;
        //    }

        //    //========================================================
        //    //Tools
        //    tempa = "";
        //    tempb = "";
        //    //Query Tools
        //    SqlCommand cmd11 = new SqlCommand("select RecorderID,EvalDateTime from Tools where CID = @CID and CaseNo = @CaseNo", scon);
        //    cmd11.Parameters.AddWithValue("@CID", CID);
        //    cmd11.Parameters.AddWithValue("@CaseNo", caseNumber);
        //    SqlDataReader reader11 = cmd11.ExecuteReader();
        //    while (reader11.Read() != false)
        //    {
        //        tempa = reader11["RecorderID"].ToString();
        //        tempb = reader11["EvalDateTime"].ToString();
        //    }
        //    reader11.Close();
        //    if (tempa.Equals(""))
        //    {
        //        ;
        //    }
        //    else
        //    {
        //        SqlCommand cmd12 = new SqlCommand("select StaffName from Staff where StaffID = @StaffID", scon);
        //        cmd12.Parameters.AddWithValue("@StaffID", tempa);
        //        SqlDataReader reader12 = cmd12.ExecuteReader();
        //        while (reader12.Read() != false)
        //        {
        //            tempa = reader12["StaffName"].ToString();
        //        }
        //        reader12.Close();
        //        dt.Rows.Add();
        //        dt.Rows[indexrow]["Evaluation"] = "บริการที่เคยได้รับ";
        //        dt.Rows[indexrow]["RecordStaff"] = tempa;
        //        dt.Rows[indexrow]["EvalDateTime"] = tempb;
        //        indexrow++;
        //    }
        //    //Bind DT in Gridview
        //    GridView1.DataSource = dt;
        //    GridView1.DataBind();
        //}

        //private void BindPlanning(string caseNumber,string CID)
        //{
        //    //Show Information Data
        //    List<string> x5 = new List<string>();
        //    SqlCommand cmd = new SqlCommand("select Co1Code,Co2Code,Co3Code,StaffID,HostCode,EvalDateTime from Evaluation where CID=@CID and CaseNo=@CaseNO", scon);
        //    DataTable dt = new DataTable();
        //    dt.Columns.Add("Co1Code");
        //    dt.Columns.Add("Co2Code");
        //    dt.Columns.Add("Co3Code");
        //    dt.Columns.Add("StaffID");
        //    dt.Columns.Add("HostCode");
        //    dt.Columns.Add("EvalDateTime");
        //    cmd.Parameters.AddWithValue("@CID", CID);
        //    cmd.Parameters.AddWithValue("@CaseNO", caseNumber);
        //    SqlDataReader reader = cmd.ExecuteReader();
        //    while (reader.Read() != false)
        //    {
        //        //tempa = reader["StaffName"].ToString();
        //        dt.Rows.Add(reader["Co1Code"], reader["Co2Code"], reader["Co3Code"], reader["StaffID"], reader["HostCode"],reader["EvalDateTime"]);
        //    }
        //    reader.Close();
        //    for (int i = 0; i < dt.Rows.Count; i++)
        //    {
        //        //string hostCode = dt.Rows[i]["HostCode"].ToString();
        //        SqlCommand cmd2 = new SqlCommand("select StaffName from Staff where StaffID = @Co1Staff or StaffID = @Co2Staff or StaffID = @Co3Staff or StaffID = @StaffID", scon);
        //        cmd2.Parameters.AddWithValue("@Co1Staff", dt.Rows[i]["Co1Code"]);
        //        cmd2.Parameters.AddWithValue("@Co2Staff", dt.Rows[i]["Co2Code"]);
        //        cmd2.Parameters.AddWithValue("@Co3Staff", dt.Rows[i]["Co3Code"]);
        //        cmd2.Parameters.AddWithValue("@StaffID", dt.Rows[i]["StaffID"]);
        //        SqlDataReader reader2 = cmd2.ExecuteReader();
        //        while (reader2.Read() != false)
        //        {
        //            x5.Add(reader2["StaffName"].ToString());
        //        }
        //        reader2.Close();
        //        //==========================================================
        //        SqlCommand cmd3 = new SqlCommand("select HostName from Host where HostID = @HostCode", scon);
        //        cmd3.Parameters.AddWithValue("@HostCode", dt.Rows[i]["HostCode"]);
        //        SqlDataReader reader3 = cmd3.ExecuteReader();
        //        while (reader3.Read() != false)
        //        {
        //            Label17.Text = reader3["HostName"].ToString();
        //        }
        //        reader3.Close();
        //        //==========================================================
        //        Label18.Text = dt.Rows[i]["EvalDateTime"].ToString();
                
        //    }
            
        //    for (int i = 0; i < x5.Count; i++)
        //    {
        //        if (x5.Count == 4)
        //        {
        //            Label13.Text = x5[0];
        //            Label14.Text = x5[1];
        //            Label15.Text = x5[2];
        //            Label16.Text = x5[3];
        //        }
        //        else
        //        {
        //            Label13.Text = "การคัดกรองนี้มีสตาฟไม่ครบสี่คน"; 
        //        }
        //    }
        //    //==============================================================
        //    //Show Table @Gridview2
        //    DataTable dx = new DataTable();
        //    DataTable tempPSNumber = new DataTable();
        //    DataTable temp = new DataTable();
        //    dx.Columns.Add("Service");
        //    dx.Columns.Add("EvalDateTime");
        //    dx.Columns.Add("Activity");

        //    x4.Clear();
        //    x5.Clear();

        //    temp.Columns.Add("ACTCode");
        //    temp.Columns.Add("EvalDateTime");
        //    temp.Columns.Add("Note");

        //    tempPSNumber.Columns.Add("PSNumber");

        //    SqlCommand cmd5 = new SqlCommand("select PSNumber from ServiceProvisionVsPerson where CID = @CID and CaseNo = @CaseNo", scon);
        //    cmd5.Parameters.AddWithValue("@CID", CID);
        //    cmd5.Parameters.AddWithValue("@CaseNo", caseNumber);
        //    SqlDataReader reader5 = cmd5.ExecuteReader();
        //    while (reader5.Read() != false)
        //    {
        //        tempPSNumber.Rows.Add(reader5["PSNumber"]);
        //    }
        //    reader5.Close();
        //    for (int i = 0; i < tempPSNumber.Rows.Count; i++)
        //    {
                
        //        SqlCommand cmd6 = new SqlCommand("select ServiceProvisionDetails.ACTCode,ServiceProvision.PSDateTime,ServiceProvisionDetails.Note from ServiceProvision inner join ServiceProvisionDetails on ServiceProvision.PSNumber = ServiceProvisionDetails.PSNumber where ServiceProvision.PSNumber = @PSNumber", scon);
        //        cmd6.Parameters.AddWithValue("@PSNumber", tempPSNumber.Rows[i]["PSNumber"]);
        //        SqlDataReader reader6 = cmd6.ExecuteReader();
        //        while (reader6.Read() != false)
        //        {
        //            temp.Rows.Add(reader6["ACTCode"], reader6["PSDateTime"], reader6["Note"]);
        //        }
        //        reader6.Close();
        //    }

        //    for (int i = 0; i < temp.Rows.Count; i++)
        //    {
        //        SqlCommand cmd7 = new SqlCommand("select ACTDesc from Activity  where Activity.ACTCode = ''", scon);
        //        cmd7.Parameters.AddWithValue("@ACTCode", temp.Rows[i]["ACTCode"]);
        //        SqlDataReader reader7 = cmd7.ExecuteReader();
        //        while (reader7.Read() != false)
        //        {
        //            dx.Rows.Add();
        //            dx.Rows[i]["Service"] = reader7["ACTDesc"].ToString();
        //            dx.Rows[i]["EvalDateTime"] = temp.Rows[i]["EvalDateTime"];
        //            dx.Rows[i]["Activity"] = temp.Rows[i]["Note"];
        //        }
        //    }

        //    GridView2.DataSource = dx;
        //    GridView2.DataBind();
        //}

    }
}